package com.coocaa.typhoon.api.v1.userfile;

import com.coocaa.cirrus.common.base.model.RespBase;
import com.coocaa.cirrus.common.base.util.JsonUtil;
import com.coocaa.typhoon.api.v1.userfile.model.input.GetSearchDataModel;
import com.coocaa.typhoon.api.v1.userfile.model.input.GetTaskDataModel;
import com.coocaa.typhoon.api.v1.userfile.model.input.UploadDataModel;
import com.coocaa.typhoon.api.v1.userfile.model.output.SearchResultMgrModel;
import com.coocaa.typhoon.api.v1.userfile.model.output.TaskProgressResultModel;
import com.coocaa.typhoon.api.v1.userfile.model.output.UploadResultModel;
import com.coocaa.typhoon.api.v1.userfile.service.UserFileService;
import com.coocaa.typhoon.api.v1.userfile.service.UserSearchMgrService;
import com.coocaa.typhoon.api.v1.userfile.util.UserFileParamCheckUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2025/9/18 09:34
 * @description
 */
@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping(value = "/v1/userFile", method = RequestMethod.POST)
@Tag(name = "用户文件")
public class UserFileController {
    @Autowired
    private UserFileService userFileService;
    @Autowired
    private UserSearchMgrService userSearchMgrService;


    @Operation(summary = "上传文件")
    @PostMapping(value = "/uploadBatch")
    @Parameters({
            @Parameter(name = "uploadType", example = "1", description = "类型，1-用户文件，2-用户搜索的文件"),
            @Parameter(name = "outType", example = "1", description = "输出类型，1-输出关联的图片")
    })
    public RespBase<List<UploadResultModel>> uploadBatch(@RequestParam("files") MultipartFile[] files,
                                                         @RequestHeader String userId,
                                                         @RequestParam Integer uploadType,
                                                         @RequestParam Integer outType,
                                                         HttpServletRequest request) throws Exception {
        log.info("请求参数: userId: {}: uploadType: {}", userId, uploadType);
        UploadDataModel dataModel = UploadDataModel.builder()
                .uploadType(uploadType)
                .outType(outType)
                .userId(userId)
                .build();
        List<UploadResultModel> res = userFileService.uploadBatch(files, dataModel);
        return new RespBase<List<UploadResultModel>>().setData(res).setSuccessResult();

    }

    @Operation(summary = "查询上传进度")
    @PostMapping(value = "/progress")
    public RespBase<List<TaskProgressResultModel>> progress(@RequestBody GetTaskDataModel dataModel, HttpServletRequest request) {
        log.info("请求参数: " + JsonUtil.obj2Json(dataModel));
        List<TaskProgressResultModel> res = userFileService.progress(dataModel);
        return new RespBase<List<TaskProgressResultModel>>().setData(res).setSuccessResult();

    }


    @Operation(summary = "多模态搜索")
    @PostMapping(value = "/searchMultiModal")
    public RespBase<Object> searchMultiModal(@RequestBody GetSearchDataModel dataModel, HttpServletRequest request) {
        log.info("请求参数: " + JsonUtil.obj2Json(dataModel));
        UserFileParamCheckUtil.check(dataModel);
        dataModel.setText(UserFileParamCheckUtil.retainLettersNumbersChinese(dataModel.getText()));
        SearchResultMgrModel res = userSearchMgrService.searchMultiModal(dataModel);
        return new RespBase().setData(res).setSuccessResult();
    }

}
